<template>
  <MkDialogEdit ref="dialogRef" v-model:isEdit="ctx.isEdit" @submit="submit">
    <MkForm :model="ctx.entity" ref="formRef" formId="BasShuXueJianYan">
      <MkInput label='名称' v-model='ctx.entity.mingCheng' prop='mingCheng' :isEdit='ctx.isEdit' />
      <MkInput label='单位' v-model='ctx.entity.danWei' prop='danWei' :isEdit='ctx.isEdit' />
      <MkInput label='选项数组' v-model='ctx.entity.xuanXiangShuZu' prop='xuanXiangShuZu' :isEdit='ctx.isEdit' />
      <MkInput label='参考范围' v-model='ctx.entity.canKaoFanWei' prop='canKaoFanWei' :isEdit='ctx.isEdit' />
      <MkInput label='Lis代码' v-model='ctx.entity.lisDaiMa' prop='lisDaiMa' :isEdit='ctx.isEdit' />
      <MkInput label='Lis名称' v-model='ctx.entity.lisMingCheng' prop='lisMingCheng' :isEdit='ctx.isEdit' />
      <MkInput label='是否必填' v-model='ctx.entity.shiFouBiTian' prop='shiFouBiTian' :isEdit='ctx.isEdit' />
      <MkInput label='公式代码(输血指征用)' v-model='ctx.entity.gongShiDaiMa' prop='gongShiDaiMa' :isEdit='ctx.isEdit' />
      <MkInput label='必填公式' v-model='ctx.entity.biTianGongShi' prop='biTianGongShi' :isEdit='ctx.isEdit' v-if="false" />
      <MkInput label='上限公式' v-model='ctx.entity.shangXianGongShi' prop='shangXianGongShi' :isEdit='ctx.isEdit'
        placeholder="例如: val > 130" />
      <MkInput label='下限公式' v-model='ctx.entity.xiaXianGongShi' prop='xiaXianGongShi' :isEdit='ctx.isEdit'
        placeholder="例如: val < 50" />
      <MkInput label='阳性公式' v-model='ctx.entity.yangXingGongShi' prop='yangXingGongShi' :isEdit='ctx.isEdit'
        placeholder="例如: val != '阴'" />
      <MkInput label='排序' v-model='ctx.entity.orderNo' prop='orderNo' :isEdit='ctx.isEdit' />
      <MkInput label='备注' v-model='ctx.entity.remark' prop='remark' :isEdit='ctx.isEdit' />
      <MkIsEnabled label='是否启用' v-model='ctx.entity.isEnabled' prop='isEnabled' :isEdit='ctx.isEdit' />
    </MkForm>
  </MkDialogEdit>
</template>

<script lang='ts' setup>
import { BasShuXueJianYanApi, BasShuXueJianYanOutput } from '/@/api/bas/BasShuXueJianYan';
import MkForm from '/@/componentsMaike/forms/MkForm.vue';
import { isNull, clone } from '/@/utils';

const emits = defineEmits(['query'])
const ctx = reactive({
  isEdit: false,
  entity: {} as BasShuXueJianYanOutput,
});

const dialogRef = ref();
const formRef = ref<InstanceType<typeof MkForm>>();

/** 打开弹框 */
const open = async (row: BasShuXueJianYanOutput) => {
  dialogRef.value.show();
  if (isNull(row?.id)) {
    ctx.isEdit = true;
    ctx.entity = { isEnabled: true, orderNo: '500' } as BasShuXueJianYanOutput;
  } else {
    ctx.isEdit = false;
    ctx.entity = clone(row); // 不能直接引用
  }
};

const close = () => {
  dialogRef.value?.close();
  emits('query')
};

const submit = async () => {
  if (!ctx.isEdit) {
    close();
    return;
  }
  // 表单验证
  const validate = await formRef?.value?.validate();
  if (!validate) return;
  if (isNull(ctx.entity.id)) {
    await BasShuXueJianYanApi.add(ctx.entity);
  } else {
    await BasShuXueJianYanApi.update(ctx.entity);
  }
  close();
};

defineExpose({ open })
</script>
